﻿package {
	//可视化相关类
	import flash.display.Sprite;
	//声音处理类
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundLoaderContext;
	import flash.media.SoundMixer;
	import flash.media.SoundTransform;
	//事件类
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	//加载相关类
	import flash.net.URLRequest;
	//外发光滤镜类
	import flash.filters.GlowFilter;

	//类及变量声明
	//======================================================================================//
	public class PlayBar extends Sprite {
		//声音对象
		private var playSound:Sound;
		//正在播放的声音
		private var playChannel:SoundChannel;
		//加载进度计时器
		private var loadTimer:Timer;
		//已下载进度条
		private var bar:Sprite=new Sprite  ;
		//进度条
		private var bars:Sprite=new Sprite  ;
		//播放头
		private var playgo:Sprite=new Sprite  ;
		//播放头滤镜(白色外发光)
		private var playgoGlowFilter:GlowFilter=new GlowFilter(0xFFFFFF,.75);
		//播放条宽度
		private var _Width:Number=190;
		//声音总长
		private var _soundLength:Number;

		//======================================================================================//
		//构造函数
		public function PlayBar() {
		}
		//初始化
		private function init() {
			//进度条总长
			bar.graphics.beginFill(0x666666);
			bar.graphics.drawRect(0,0,_Width,0.5);
			bar.graphics.endFill();
			addChild(bar);
			//已下载的长度
			bars.graphics.beginFill(0xCCCCCC);
			bars.graphics.drawRect(0,0,_Width,0.5);
			bars.graphics.endFill();
			addChild(bars);
			//播放头
			playgo.graphics.beginFill(0xFFFFFF);
			playgo.graphics.drawCircle(0,0,2);
			playgo.graphics.endFill();
			var FilterArr:Array=[];
			FilterArr.push(playgoGlowFilter);
			playgo.filters=FilterArr;
			addChild(playgo);
			//侦听进度的计时器
			loadTimer=new Timer(100);
			loadTimer.addEventListener(TimerEvent.TIMER,loadBar);
			loadTimer.start();
		}
		//
		//======================================================================================//
		/**
		//提供给外部设置的属性
		**/
		//================================================》
		//控制进度条的声音对象
		public function set sound(_Sound:Sound) {
			playSound=_Sound;
			//trace(playSound.length)
		}
		//================================================》
		//控制进度条的声音状态对象
		public function set channel(_channel:SoundChannel) {
			playChannel=_channel;
		}
		//================================================》
		//进度条的总宽度
		//设置宽度后调用初始函数
		public function set Width(Width:Number) {
			_Width=Width;
			init();
			//trace(playSound.length)
		}
		//======================================================================================//
		/**
		//提供给外部只读属性
		**/
		//================================================》
		//声音总长
		public function get soundLength():Number {
			return _soundLength;
		}
		/**
		以下为类内部函数
		**/
		//======================================================================================//
		//加载进度
		private function loadBar(evt:TimerEvent) {
			//已下载字节
			var loaded:int=playSound.bytesLoaded;
			//总字节
			var total:int=playSound.bytesTotal;
			//下载百分比
			var loadbfb:Number=loaded / total;
			//已下载进度条宽度
			var loadedBar:uint=Math.floor(loadbfb * _Width);
			bars.width=loadedBar;
			//当前播放的时间
			var playtime:int=playChannel.position;
			//声音总时间
			var timelength:int=playSound.length;
			timelength/= loadbfb;
			_soundLength=timelength;
			//播放头位置
			playgo.x=playtime / timelength * _Width;
			//trace("总时间："+timelength)
			//trace("当前播放:"+playtime)
		}
	}
}